<template>
<view class="container" :hidden="hide">
        <view class="weui-media-box__bd w_3 flex">
            <view class="weui-media-box__title color_9 font_b font_size15 flex_one">
                <view class="flex_one h4">是否开启自动开门功能</view>
            </view>
            <view class="weui-media-box__desc all">
                <switch :checked="is_set_open" bindchange="set_open_msg"></switch>
            </view>
        </view>
        <view class="detail">开启,则打开小程序后,会自动识别且打开周边符合范围的门禁设备,使用前请先打开定位</view>
</view>
</template>

"<script>
// pages/share/set.js
var app = getApp();
var common = require('../../utils/common.js');

export default {
  data() {
    return {
      is_set_open: false,
      hide: true
    };
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    common.setMainColor(that, function (config) {});
    common.post('Login&a=auto_open_door_info', {}, function (data) {
      console.log(data.auto_door_open);
      that.setData({
        is_set_open: data.auto_door_open ? data.auto_door_open : false,
        hide: false
      });
    });
  },
  components: {},
  props: {},
  methods: {
    set_open_msg: function (e) {
      console.log(e.detail.value);
      common.post('Login&a=switch_auto_open_door', {
        noTip: true
      }, function (data) {
        wx.setStorageSync('auto_door_open', e.detail.value ? '1' : '0');
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
/* pages/share/set.css */

.container{padding:10px;
background:#fff;
  margin-top:20px
}

.flex {
display:flex !important;
display:-webkit-box;
display:-moz-box;
display:-ms-flexbox;
display:-webkit-flex;
align-items:center;
}


.flex_one {
flex:1;
-webkit-box-flex:1;
-webkit-flex:1;
}
.weui-media-box__title .h4 {
font-size:16px;

}
.detail{
  padding-top:5px;
color:#999;

}



</style>